package com.yf.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yf.domain.OdOrderLngMoney;
import com.yf.redis.DistributedRedisLock;
import com.yf.redis.RedisKey;
import com.yf.service.OdOrderLngMoneyService;
import io.gitee.loulan_yxq.owner.core.collection.CollTool;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/*********************************************************
 ** 加气金定时任务
 ** <br><br>
 ** @author: yangfeng
 ** @date: 2024/3/27 11:32
 ** @version: 1.0.0
 *********************************************************/
@Component
@Slf4j
public class LngMoneyTask {
    @Resource
    private OdOrderLngMoneyService odOrderLngMoneyService;
    @Resource
    private DistributedRedisLock distributedRedisLock;

    @Resource
    private RedissonClient redissonClient;
    @Transactional(propagation = Propagation.REQUIRED)
//    @Scheduled(cron="0/10 * * * * ?")//每秒钟执行一次，以空格分隔
    public void cron(){
        LambdaQueryWrapper<OdOrderLngMoney> q = new LambdaQueryWrapper<OdOrderLngMoney>();
        q.select(OdOrderLngMoney::getUuid).eq(OdOrderLngMoney::getStatus,0);
        List<OdOrderLngMoney> list = odOrderLngMoneyService.list(q);
        log.info("获取到的数据为 data={}", list);
        if(CollTool.isNotEmpty(list)){
            list.forEach(t->{
                //执行逻辑
                update(t);
//                try {
//
//                } catch (Exception e) {
//                    log.error("数据更新出错",e);
//                    throw new RuntimeException();
//                }
            });
        }

    }
//    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void update(OdOrderLngMoney t){
//        RLock lock = redissonClient.getLock(RedisKey.LNG_MONEY + t.getUuid());
//        boolean b1 = false;
//        try {
//            b1 = lock.tryLock(0, 5, TimeUnit.SECONDS);
//            if(b1){
//                OdOrderLngMoney odOrderLngMoney = odOrderLngMoneyService.getById(t.getUuid());
//                if(odOrderLngMoney.getStatus().equals(0)){
//                    odOrderLngMoney.setStatus(1);
//                    odOrderLngMoneyService.updateById(odOrderLngMoney);
//                }
//            }
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        } finally {
//            if(b1){
//                lock.unlock();
//            }
//        }
        boolean b = distributedRedisLock.lock(RedisKey.LNG_MONEY+t.getUuid(),5,10, TimeUnit.SECONDS);
        if(b){
            try {
                OdOrderLngMoney odOrderLngMoney = odOrderLngMoneyService.getById(t.getUuid());
                if(odOrderLngMoney.getStatus().equals(0)){
                    odOrderLngMoney.setStatus(1);
                    odOrderLngMoneyService.updateById(odOrderLngMoney);
                }
            } catch (Exception e) {
//                log.error("数据更新异常",e);
                throw new RuntimeException(e);
            } finally {
                if(b){
                    distributedRedisLock.unlock(RedisKey.LNG_MONEY+t.getUuid());
                }
            }
        }else{
            log.warn("获取锁失败");
        }

    }
}
